package com.github.tangmonkmeat;

import sun.awt.X11.XUnmapEvent;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/5/9 上午10:37
 */
public class 人口最多的年份 {

    public static void main(String[] args) {

        int i = new 人口最多的年份().maximumPopulation(new int[][]{{1950, 1961}, {1960, 1971}, {1970, 1981}});
        System.out.println(i);

    }

    public int maximumPopulation(int[][] logs) {
        int maxNum = -1;
        int currMax;
        int x = -1;

        int min_x = Integer.MAX_VALUE;
        int max_x = Integer.MIN_VALUE;
        for(int[] log : logs){
            if (log[0] < min_x){
                min_x = log[0];
            }
            if (log[1] > max_x){
                max_x = log[1];
            }
        }

        for (int i = min_x; i <= max_x; i++) {
            currMax = 0;
            for (int[] log : logs) {
                if (log[0] <= i && log[1] - 1 >= i) {
                    currMax++;
                }
            }
            if (currMax > maxNum){
                maxNum = currMax;
                x = i;
            }
        }
        return x;
    }
}
